package com.code.generator.cn.service.impl;

import com.code.generator.cn.entity.CoderField;
import com.code.generator.cn.mapper.CoderFieldMapper;
import com.code.generator.cn.service.ICoderFieldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class CoderFieldServiceImpl implements ICoderFieldService {

    @Autowired
    private CoderFieldMapper coderFieldMapper;

    @Override
    public List<CoderField> getListByTableId(String id) {
        return coderFieldMapper.getListByTableId(id);
    }

    @Override
    public void saveList(List<Map<String, Object>> columns) {
        coderFieldMapper.saveList(columns);
    }

    @Override
    public void delete(CoderField coderField) {
        coderFieldMapper.deleteByPrimaryKey(coderField.getId());
    }


    @Transactional
    @Override
    public   List<CoderField>  getAllFields(String id, List<Map<String, Object>> columns) {
        List<CoderField> coderFields=getListByTableId(id);
        if (coderFields.size() == 0) {
            saveList(columns);
        } else {
            //同步多余的字段
            deleteFieldList(columns, coderFields);
            //追加不存在的字段
            createFieldList(columns, coderFields);
        }
        coderFields = getListByTableId(id);
        return coderFields;
    }

    @Override
    public Object getById(String id) {
        return coderFieldMapper.selectByPrimaryKey(Integer.parseInt(id));
    }

    @Override
    public void update(CoderField coderField) {
        coderFieldMapper.updateByPrimaryKeySelective(coderField);
    }

    private void createFieldList(List<Map<String, Object>> columns, List<CoderField> coderFields) {
        List<Map<String, Object>> fieldList = new ArrayList<>();

        a: for (Map<String, Object> column : columns) {
            String fieldName = column.get("fieldName").toString();
            for (int i = 0; i < coderFields.size(); i++) {
                if(coderFields.get(i).getFieldName().equals(fieldName)){
                    continue a;
                }
                if (i == coderFields.size() - 1) {
                    if (!fieldName.equals(coderFields.get(i).getFieldName())) {
                        fieldList.add(column);
                    }
                }

            }
        }
        if(fieldList.size()>0){
            saveList(fieldList);
        }
    }


    /**
     * 删除多余的数据库字段
     * @param columns
     * @param coderFields
     * @return
     */
    private  void deleteFieldList(List<Map<String, Object>> columns, List<CoderField> coderFields) {
        List<CoderField> delFieldList = new ArrayList<>();
        a:
        for (CoderField coderField : coderFields) {
            String fieldName = coderField.getFieldName();
            for (int i = 0; i < columns.size(); i++) {
                String dataFieldName = columns.get(i).get("fieldName").toString();
                if (fieldName.equals(dataFieldName)) {
                    continue a;
                }
                if (i == columns.size() - 1) {
                    if (!coderField.equals(dataFieldName)) {
                        delFieldList.add(coderField);
                    }
                }
            }
        }
        for (CoderField coderField : delFieldList) {
            delete(coderField);
        }

    }
}
